package com.dc.di.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.apache.commons.compress.bzip2.*;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.eclipse.jdt.core.dom.ThisExpression;

public class CompressData {

	/**
	 * @param args
	 */
private String comtype="";

	public CompressData(String str){
		this.comtype=str;
	}
	public byte[] compress(byte[] by,int i){
		ByteArrayOutputStream baos = new ByteArrayOutputStream(i/10);
		byte[] reby=null;
		if("GZIP".equalsIgnoreCase(this.comtype)){
			GZIPOutputStream gzos = null;
			try {
			gzos= new GZIPOutputStream(baos);
			gzos.write(by);
			} catch (IOException e) {
				e.printStackTrace();
			}
			finally{
				try {
					gzos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			reby=baos.toByteArray();
		}else if("ZIP".equalsIgnoreCase(this.comtype)){
			ZipOutputStream zos=null;
			try {
			zos= new ZipOutputStream(baos);
			ZipEntry entry = new ZipEntry("zip");
			entry.setSize(i);
			zos.putNextEntry(entry);
			zos.write(by);
			}catch(IOException e) {
			e.printStackTrace();
			}
			finally{
			try {
				zos.closeEntry();		
				zos.close();			
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
			reby=baos.toByteArray();				
		}else if("BZIP2".equalsIgnoreCase(this.comtype)){
			CBZip2OutputStream cbo =null;
		  try {
			 cbo = new CBZip2OutputStream(baos);
			 cbo.write(by);
			} catch (IOException e) {
				e.printStackTrace();
			}
			finally{
				try {
					cbo.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			reby=baos.toByteArray();
		}
		return reby;
		
	}
	public byte[] uncompress(byte[] by){
		byte[] reby=null;
		if("GZIP".equalsIgnoreCase(this.comtype)){
			GZIPInputStream is=null;
			try {
				is = new GZIPInputStream(new ByteArrayInputStream(by));
				reby=IOUtils.toByteArray(is);
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}else if("ZIP".equalsIgnoreCase(this.comtype)){
			ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(by));
			try {
				if(zis.getNextEntry()!=null){
					reby=IOUtils.toByteArray(zis);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				try {
					zis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}else if("BZIP2".equalsIgnoreCase(this.comtype)){
			CBZip2InputStream is=null;
			try {
				is = new CBZip2InputStream(new ByteArrayInputStream(by));
				reby=IOUtils.toByteArray(is);
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}		
		}
		return reby;
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
